#! /bin/bash

#set -x

PGNAME=`basename $0`
PGDIRNAME=`dirname $0`
PGERR="$PGNAME[$$]: "
PGUSER=`id -un`

if [ "${MLPROD_HOME}" == "" ]
then
  echo "${PGERR} :  MLPROD_HOME env not set;  exiting !!!" >&2
  exit 10
fi

PGCFG_FILE="${MLPROD_HOME}/config/mlprog.rc"
if [ ! -f ${PGCFG_FILE} ]
then
  echo "${PGERR} : config file <${PGCFG_FILE}> not found; exiting !!!" >&2
  exit 10
fi

. "${PGCFG_FILE}"

CUST_CTX="$(mlget_cust_ctx 'cust')"
MOD_CTX="$(mlget_cust_ctx  'mod')"
BATCH_WFL_ID=1                  # workflow/product id
BATCH_MODE="oper"
if [ ! -z ${BATCH_RUN_MODE} ]
then
  BATCH_MODE="${BATCH_RUN_MODE}"
fi
LOG_CTX="${BATCH_MODE}-${CUST_CTX}"
LOG_CTX="${LOG_CTX}-$1"


PGCFG_FILE="${MLPROD_HOME}/cust/${CUST_CTX}/config/${MOD_CTX}.rc"
if [ ! -f ${PGCFG_FILE} ]
then
  echo "${PGERR} : config file <${PGCFG_FILE}> not found; exiting !!!" >&2
  exit 10
fi

. "${PGCFG_FILE}"
if [ ! -f ${PGCFG_FILE} ]
then
  echo "${PGERR} : config file sourcing <${PGCFG_FILE}> error; exiting !!!" >&2
  exit 10
fi


function usage()
{
  echo "Usage: $PGNAME  BATCH_RECV_DATE  ftp_request_file";
}


function to_mmddyy_hhmmss_date {
	declare mydate=$1
	declare month=` echo $mydate | cut -d'/' -f1`
	declare day=`   echo $mydate | cut -d'/' -f2`
	declare year=`  echo $mydate | cut -d'/' -f3 | cut -d' ' -f1`
        declare hour=`  echo $mydate | cut -d' ' -f2 | cut -d':' -f1`
        declare min=`   echo $mydate | cut -d' ' -f2 | cut -d':' -f2`
        declare sec=`   echo $mydate | cut -d' ' -f2 | cut -d':' -f3`

	year=` echo $year | sed -e 's/.*\([[:digit:]]\{2\}\)$/\1/' `
	declare retv=` printf "%s%s%s_%s%s%s" $month $day $year $hour $min $sec`
	echo $retv
}

function create_ftp_request {
	declare record=$1
	declare rq_file=$2
	#-------------------------------------------------
	# -f1 is a int, but no use here
	#  --input format 'int|date|custid|tpl|medicare_slow|medicare_fast|medicaid' 
	#-------------------------------------------------
	declare -a 	F[1]=` echo $record | cut -d'|' -f2` \
		 		F[2]=` echo $record | cut -d'|' -f3` \
				F[3]=` echo $record | cut -d'|' -f4` \
				F[4]=` echo $record | cut -d'|' -f5` \
				F[5]=` echo $record | cut -d'|' -f6` \
				F[6]=` echo $record | cut -d'|' -f7` 

	declare date6=` to_mmddyy_hhmmss_date "${F[1]}" `
	declare basename=` printf "payorintel_%s_%06d_emdeon" $date6 ${F[2]} `

	declare result=""

	# processing TPL 
	if [ ${F[3]} -gt 0 ]; then
		result="${basename}_tpl_tpl-rt.del.pgp"
	fi
		
	# processing MEDICARE_SLOW
	if [ ${F[4]} -gt 0 ]; then
		if [ -n "$result" ]; then
			result="${result},"
		fi
		result="${result}${basename}_medicare.del.pgp"
	fi
		
	# processing MEDICARE_FAST
	if [ ${F[5]} -gt 0 ]; then
		if [ -n "$result" ]; then
			result="${result},"
		fi
		result="${result}${basename}_medx.del.pgp"
	fi
	# processing MEDICAID
	if [ ${F[6]} -gt 0 ]; then
		if [ -n "$result" ]; then
			result="${result},"
		fi
		result="${result}${basename}_.del.pgp"
	fi
	
	echo "$result" > $rq_file
	while [ ! -f $rq_file ]; do
		# ftp request file may fail due to NFS IO error
		# Just wait short time, try again.
		sleep 37;
		echo "$result" > $rq_file
	done

	mllogerr "[${LOG_CTX}] : INF : $rq_file just created !!! ";
}



#====================================================

if [ $# -ne 2 ]; then
  usage;
  exit 10;
fi

declare -r LAST_RUN_DATE=$1
declare -r FTP_REQUEST_FILE=$2

if [ -f "${FTP_REQUEST_FILE}" ]; then
	exit 1;		 # emdeon ftp request has been created
fi

ML_tmp_dir="$MLPROD_HOME/tmp"

$MLBIN_DIR/pgsql -f ${SQL_CTX_RUNT_DIR}/get_tpl_medicx_number.sql \
	> "$ML_tmp_dir/${LAST_RUN_DATE}.output" 2>/dev/null 

exec 3<>  "$ML_tmp_dir/${LAST_RUN_DATE}.output"
while read x <&3
do
	#---------------------------------------
	# last_run_date is -f2 ( field 2)
	#  --input format 'int|date|custid|tpl|medicare_slow|medicare_fast|medicaid' 
	#---------------------------------------
	LRD=` echo $x | cut -d'|' -f2` 
	LRD=` to_mmddyy_hhmmss_date "$LRD" `
	lCUSTID=` echo $x | cut -d'|' -f3` 
	lCUSTID=` printf "%06d" $lCUSTID ` 

	if [ "${LRD}_${lCUSTID}" = "$LAST_RUN_DATE" ]; then
		create_ftp_request "$x"  "${FTP_REQUEST_FILE}"
		echo  "${FTP_REQUEST_FILE}"    # confirm filename
	else
		mllogerr "[${LOG_CTX}] : INF : No matching DB record for $LAST_RUN_DATE ";
	fi

	break	# only one row
done
exec 3>&-
rm -f "$ML_tmp_dir/${LAST_RUN_DATE}.output"

exit 0

